<?php
declare (strict_types=1);

namespace app\user\service;

use app\model\Chat;
use app\model\ChatLog;
use app\model\Kefu;
use app\model\UserQueue;

class KefuService
{
    /**
     * 客服登录
     * @param $data
     * @return mixed|string
     * @throws \Exception
     * @author dgc<453342213@qq.com>
     * createTime:2020/8/24 11:57
     */
    public function Login($data)
    {
        $where[] = ['username|phone', '=', $data['username']];
        $info = $this->getInfo($where, 'id,token,phone,name,head,password,status,salt');
        //删除之前的缓存
        if (!$info) {
            throw  new \Exception('用户不存在或密码错误', -1);
        }
        if ($info->status != 1) {
            throw  new \Exception('账号已被禁用，如有需要请联系管理员！', -1);
        }
        if ($info->password != setPassword($data['password'], $info->salt)) {
            throw  new \Exception('用户不存在或密码错误', -1);
        }
        $toInfo = $info->toArray();
        unset($toInfo['password']);
        unset($toInfo['token']);
        unset($toInfo['salt']);
        //删除之前的缓存
        redisHdel('login_info', 'kf_' . $info->token);
        $info->token = md5(sha1($info->id . time() . rand(1, 9999)));
        $info->ip = getClientIp();
        $info->save();
        redisHset('login_info', 'kf_' . $info->token, $toInfo);
        return $info->token;
    }

    /**
     * 获取聊天对话
     * @param $user
     * @return array
     * @author dgc<453342213@qq.com>
     * createTime:2020/8/24 14:15
     */
    public function chatList($user)
    {
        $page = getPage();
        $model = new Chat();
        $list = $model->where('kefu_id', $user['id'])
            ->with('userInfo')
            ->page($page['page'], $page['list_rows'])
            ->order('u_time desc')
            ->select()->toArray();
        foreach ($list as $k => $v) {
            $list[$k]['is_unread'] =0;
        }
        return $list;
    }

    /**
     * 获取聊天记录
     * @param $chatId 聊天id
     * @return array
     * @author dgc<453342213@qq.com>
     * createTime:2020/8/24 14:24
     */
    public function chatLogList($chatId)
    {
        $page = getPage();
        $model = new ChatLog();
        $list = $model->where('chat_id', $chatId)
            ->page($page['page'], $page['list_rows'])
            ->order('id desc')
            ->select()
            ->toArray();
        sort($list);
        return $list;
    }

    /**
     * 获取客服单挑数据
     * @param $where
     * @param string $field
     * @return array|\think\Model|null
     * @author dgc<453342213@qq.com>
     * createTime:2020/8/24 11:57
     */
    public function getInfo($where, $field = "*")
    {
        return (new Kefu())->where($where)
            ->field($field)
            ->find();
    }

    /**
     * 修改客服状态!
     * @param $uid
     * @param $data
     */
    public static function saveKefu($where, $data)
    {
        return Kefu::where($where)->update($data);
    }

    /**
     * 获取客服正在服务的游客
     * @param $kefu_code  客服编码
     */
    public static function getChatList($where)
    {
        return Chat::where($where)
            ->where('reception_status', 1)
            ->select();
    }

    /**
     * 更新chat
     * @param $kefu_id
     * @param $param
     * @return Chat
     * @author dgc<453342213@qq.com>
     * createTime:2020/8/31 15:30
     */
    public static function saveChat($where, $param)
    {
        return Chat::where($where)
            ->where('reception_status', 1)
            ->update($param);
    }

    public static function setReceptionStatus($visitor_id, $status, $fd = 0)
    {
        return Db::name('visitor_queue')->where('visitor_id', $visitor_id)->update(['client_id' => $fd, 'reception_status' => $status]);
    }


}
